package com.uinnova.product.eam.service.bm.impl;

import com.binary.core.util.BinaryUtils;
import com.uinnova.product.eam.model.bm.FlowModelMergeParams;
import com.uinnova.product.eam.service.IEamNoticeService;
import com.uinnova.product.eam.service.bm.FlowModelMergeSvc;
import com.uinnova.project.base.diagram.comm.model.ESDiagram;
import com.uino.bean.cmdb.base.LibType;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 流程建模发布/检出接口实现
 * @author ch
 */
@Slf4j
@Service
public class FlowModelMergeSvcImpl implements FlowModelMergeSvc {

    @Autowired
    private FlowModelMergePreProcessor preProcessor;
    @Autowired
    private FlowModelCiRltMerge ciRltMerge;
    @Autowired
    private FlowModelCatalogMerge catalogMerge;
    @Autowired
    private FlowModelDiagramMerge diagramMerge;
    @Autowired
    private FlowModelMergePostProcessor postProcessor;
    @Autowired
    @Lazy
    private IEamNoticeService eamNoticeService;

    @Override
    public void push(List<Long> dirIds, String diagramId, String desc, Long parentId) {
        FlowModelMergeParams params = preProcessor.pushBaseQuery(dirIds, Collections.singletonList(diagramId), parentId);

        params.setDesc(BinaryUtils.isEmpty(desc) ? "" : desc);
        preProcessor.sync(params, LibType.PRIVATE);
//        ciRltMerge.pushCheck(params);
//        catalogMerge.pushCheck(params);
//        diagramMerge.pushCheck(params);

        ciRltMerge.push(params);
        catalogMerge.push(params);
        diagramMerge.push(params);
        postProcessor.refreshCategory(params, LibType.DESIGN);
        postProcessor.execute(params);
        //推送工作台-消息
        List<String> msgDiagramSet = params.getMergeDiagramList().stream().map(ESDiagram::getDEnergy).collect(Collectors.toList());
        msgDiagramSet.addAll(params.getDiagramIds());
        eamNoticeService.diagramUpdateMsgSave(msgDiagramSet);
    }

    @Override
    public void pull(Long dirId, String diagramId, Long targetDirId, LibType libType) {
        long start = System.currentTimeMillis();
        ciRltMerge.pullCheck(dirId);
        long checkEnd = System.currentTimeMillis();
        log.info("校验时间：{}", checkEnd-start);
        FlowModelMergeParams params = preProcessor.pullBaseQuery(dirId, Collections.singletonList(diagramId), libType);
        long queryEnd = System.currentTimeMillis();
        log.info("查询时间：{}", queryEnd-checkEnd);
        params.setTargetDirId(targetDirId);
        preProcessor.sync(params, LibType.DESIGN);
        long syncEnd = System.currentTimeMillis();
        log.info("同步时间：{}", syncEnd-queryEnd);
        // ciRltMerge.pullCheck(params, dirId, diagramId);
        catalogMerge.pullCheck(params);
        diagramMerge.pullCheck(params);
        catalogMerge.pull(params, libType);
        long categoryEnd = System.currentTimeMillis();
        log.info("目录时间：{}", categoryEnd-syncEnd);
        ciRltMerge.pull(params);
        long pullStart = System.currentTimeMillis();
        log.info("ciRlt时间：{}", pullStart-categoryEnd);
        diagramMerge.pull(params);
        log.info("视图检出时间：{}", System.currentTimeMillis() - pullStart);
    }
}
